package com.atmilan.quartz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;

import com.atmilan.quartz.mapper.CrmebOrderSyncMapper;
import com.atmilan.quartz.model.entity.crmeb.StoreOrder;
import com.atmilan.quartz.service.IStoreOrderService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 订单查询接口实现
 * @Description: 
 * @author daigb
 * @date 2021-01-07
 */
@Service
public class StoreOrderServiceImpl extends ServiceImpl<CrmebOrderSyncMapper, StoreOrder> implements IStoreOrderService {

    /**
     * 查询订单id数据
     */
    @Override
    public List<String> queryOrderIds(Date startTime) {
        List<StoreOrder> list = list(Wrappers.<StoreOrder>lambdaQuery()
                .select(StoreOrder :: getOrderId)
                .eq(StoreOrder :: getPaid, 1)
                .eq(StoreOrder :: getRefundStatus, 0)
                .eq(StoreOrder :: getIsDel, 0)
                .gt(StoreOrder :: getPayTime, startTime)
                );
        
        return CollectionUtils.isEmpty(list) ? new ArrayList<String>() : list.stream().map(o -> o.getOrderId()).collect(Collectors.toList());
    }
    
    /**
     * 根据订单id查询订单详情, 只取一条数据
     */
    @Override
    public StoreOrder getByOrderId(String orderId) {
        return getOne(Wrappers.<StoreOrder>lambdaQuery()
                .eq(StoreOrder :: getOrderId, orderId)
                .eq(StoreOrder :: getIsDel, false)
                .last("LIMIT 1")
                );
    }

}
